﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace UserInterface.Model
{
    using Presenters;

    public static class GameStorage
    {
        public static void SaveFileAsBinaryToBaseDirectoryOfCurrentDomain(GameInterfaceCoordinator obj)
        {
            string savePath = GetSavePath();
            SaveFile(savePath, obj);
        }
        private static string GetSavePath()
        {
            string fileName = "saveFile";
            string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

            string savePath = Path.Combine(baseDirectory, fileName);

            return savePath;
        }
        private static void SaveFile(string savePath, GameInterfaceCoordinator obj)
        {
            using (FileStream stream = new FileStream(savePath, FileMode.Create))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream,obj);
            }
        }

        public static GameInterfaceCoordinator LoadFromBaseDirectoryOfCurrentDomain()
        {
            string savePath = GetSavePath();
            GameInterfaceCoordinator coordinator = LoadFile(savePath);
            return coordinator;
        }

        private static GameInterfaceCoordinator LoadFile(string savePath)
        {
            GameInterfaceCoordinator coordinator;
            using (FileStream stream = new FileStream(savePath, FileMode.Open))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                stream.Seek(0, SeekOrigin.Begin);
                coordinator = formatter.Deserialize(stream) as GameInterfaceCoordinator;
            }
            return coordinator;
        }
    }
}
